package com.bokey.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bokey.entity.Collect;
import com.bokey.entity.Node;
import com.bokey.entity.User;
import com.bokey.service.CollectService;
import com.bokey.service.NodeService;
import com.bokey.service.UserService;
import com.bokey.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin // 跨域解决
@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;
    @Autowired
    private NodeService nodeService;
    @Autowired
    private UserService userService;

    // 判断(userId)用户是否收藏该笔记(nodeId)
    @RequestMapping("/whetherCollect/{userId}/{nodeId}")
    public boolean whetherCollect(@PathVariable Integer userId,@PathVariable Integer nodeId){
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId).eq("nodeId",nodeId);
        Collect collect = collectService.getOne(queryWrapper);
        if(collect == null) {
            return false;
        }
        return true;
    }

    // 用户(userId)收藏笔记(nodeId)
    @RequestMapping("/collectNode/{userId}/{nodeId}")
    public boolean collectNode(@PathVariable Integer userId,@PathVariable Integer nodeId) {
        Collect collect = new Collect();
        collect.setUserid(userId);
        collect.setNodeid(nodeId);
        return collectService.save(collect);
    }

    // 用户取消收藏笔记，通过用户id和笔记id
    @RequestMapping("/notCollectNode/{userId}/{nodeId}")
    public boolean notCollectNode(@PathVariable Integer userId,@PathVariable Integer nodeId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId).eq("nodeId",nodeId);
        return collectService.remove(queryWrapper);
    }

    // 通过笔记id删除该笔记的所有收藏
    public boolean deleteNodeCollect(Integer nodeId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nodeId",nodeId);
        return collectService.remove(queryWrapper);
    }

    // 查询用户收藏的所有笔记，分页
    @RequestMapping("getCollectNode/{userId}/{currentPage}/{pageSize}")
    public R getCollectNode(@PathVariable Integer userId,@PathVariable Integer currentPage,@PathVariable Integer pageSize) {
        Page<Collect> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        Page<Collect> collectPage = collectService.page(page, queryWrapper);
        List<Node> nodes = new ArrayList<>();
        for (Collect item:collectPage.getRecords()) {
            QueryWrapper<Node> nodeQueryWrapper = new QueryWrapper<>();
            nodeQueryWrapper.eq("id",item.getNodeid());
            Node node = nodeService.getOne(nodeQueryWrapper);
            nodes.add(node);
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("id",userId);
        User user = userService.getOne(userQueryWrapper);
        return R.ok().data("nodes",nodes).data("user",user);
    }

}
